﻿using System.Collections.Generic;
using System.Linq;
using BankCreditPortfolio.DAL.Entities;

namespace BankCreditPortfolio.DAL.Services
{
    public class OrderInfoService : DataService<OrderInfoPersistence>
    {
        public IQueryable<OrderInfo> GetAllByTariff(int tariffId)
        {
            var filteredQuery = this.CreateQuery().Where(x => x.Tariff.TariffId == tariffId);
            return filteredQuery.Count() > 0 ? filteredQuery : null;
        }
        
        public IQueryable<OrderInfo> GetAllByStatus(string status)
        {
            var filteredQuery = this.CreateQuery().Where(x => x.OrderStatus.Status == status);
            return filteredQuery.Count() > 0 ? filteredQuery : null;
        }

        public IQueryable<OrderInfo> GetArchiveOrders()
        {
            var filteredQuery = this.CreateQuery().Where(x => x.OrderStatus.Status == "Repayed" || 
                x.OrderStatus.Status == "Rejected");
            return filteredQuery.Count() > 0 ? filteredQuery : null;
        }
        
        public IQueryable<OrderInfo> GetAllByEmail(string email)
        {
            var filteredQuery = this.CreateQuery().Where(x => x.Client.Account.Email == email)
                .Where(x => x.OrderStatus.Status != "Approved");
            return filteredQuery.Count() > 0 ? filteredQuery : null;
        }
        
        private Tariff GetTariffObject(TariffPersistence obj)
        {
            var result = new Tariff
            {
                TariffId = obj.TariffId,
                CreditPercent = obj.CreditPercent,
                Currency = obj.Currency,
                Description = obj.Description,
                FirstPayment = obj.FirstPayment,
                Max = obj.Max,
                Min = obj.Min,
                NumberOfGuarantors = obj.NumberOfGuarantors,
                OtherPayments = obj.OtherPayments,
                Time = obj.Time,
                Title = obj.Title,
                Fine = obj.Fine,
                EarlyRepaymentFine = obj.EarlyRepaymentFine,
                Used = obj.Used
            };
            return result;
        }

        private OrderInfo GetOrderInfoObject(OrderInfoPersistence obj)
        {
            var res = new OrderInfo
                          {
                              Currency = obj.Currency,
                              Documents = obj.Documents,
                              Gains = obj.Gains,
                              //Guarantor = obj.Guarantor,
                              OrderId = obj.OrderId,
                              OrderStatus = obj.OrderStatus,
                              Tariff = GetTariffObject(obj.Tariff),
                              Client = obj.Client,
                              Amount = obj.Amount,
                              GainsCurrency = obj.GainsCurrency
                          };
            return res;
        }

        new public OrderInfo Find(int id)
        {
            return GetOrderInfoObject(base.Find(id));
        }

        new public IEnumerable<OrderInfo> GetAll()
        {
            return base.GetAll().Select(GetOrderInfoObject).ToList();
        }

        public override bool Save(ref OrderInfoPersistence entity)
        {
            var obj = GetOrderInfoObject(entity);
            var result = this.Save(ref obj);
            entity.OrderId = obj.OrderId;
            return result;
        }

        public bool Update(ref OrderInfo entity)
        {
            var item = base.Find(entity.OrderId);
            item.Amount = entity.Amount;
            item.Client = entity.Client;
            item.Currency = entity.Currency;
            item.Documents = entity.Documents;
            item.Gains = entity.Gains;
            item.GainsCurrency = entity.GainsCurrency;
            item.OrderStatus = entity.OrderStatus;
            item.Tariff = entity.Tariff;
            return base.Save(ref item);
        }

        public bool Save(ref OrderInfo entity)
        {
            var obj = new OrderInfoPersistence
                          {
                              Currency = entity.Currency,
                              Documents = entity.Documents,
                              Gains = entity.Gains,
                              //Guarantor = entity.Guarantor,
                              OrderId = entity.OrderId,
                              OrderStatus = entity.OrderStatus,
                              Client = entity.Client,
                              Amount = entity.Amount,
                              GainsCurrency = entity.GainsCurrency,
                              Tariff = new TariffPersistence
                                           {
                                               CreditPercent = entity.Tariff.CreditPercent,
                                               Currency = entity.Tariff.Currency,
                                               Description = entity.Tariff.Description,
                                               FirstPayment = entity.Tariff.FirstPayment,
                                               Max = entity.Tariff.Max,
                                               Min = entity.Tariff.Min,
                                               NumberOfGuarantors = entity.Tariff.NumberOfGuarantors,
                                               OtherPayments = entity.Tariff.OtherPayments,
                                               Time = entity.Tariff.Time,
                                               Title = entity.Tariff.Title,
                                               Fine = entity.Tariff.Fine,
                                               TariffId = entity.Tariff.TariffId,
                                               EarlyRepaymentFine = entity.Tariff.EarlyRepaymentFine,
                                               Used = entity.Tariff.Used
                                           }
                          };
            var res = base.Save(ref obj);
            entity.OrderId = obj.OrderId;
            return res;
        }

        new public IQueryable<OrderInfo> CreateQuery()
        {
            var tmpCollection1 = base.CreateQuery();
            var tmpCollection = new List<OrderInfo>();
            foreach (var orderPersistence in tmpCollection1)
            {
                tmpCollection.Add(GetOrderInfoObject(orderPersistence));
            }
            return tmpCollection.AsQueryable();
        }
    }
}